#!/bin/bash

set -ex

if [ ! -e workspace ]; then
	mkdir workspace
fi

cd workspace

echo "===================install some packages=================="
sudo apt update
sudo apt install -y git vim openssh-server
sudo apt install -y build-essential make gcc g++ bison flex libssl-dev libelf-dev cmake pigz 

echo "===================get u-boot==============================="
if [ ! -e u-boot ]; then
	git clone https://gitee.com/open-loongarch/u-boot.git
fi
echo "===================get kernel==============================="
if [ ! -e linux-6.12 ]; then
	git clone https://gitee.com/open-loongarch/linux-6.12.git
fi
echo "===================get buildroot==============================="
if [ ! -e buildroot-2024.08 ]; then
	git clone https://gitee.com/open-loongarch/buildroot-2024.08.git
fi
echo "===================get cross toolchain==============================="
if [ ! -e cross-toolchain ]; then
	git clone https://gitee.com/open-loongarch/cross-toolchain.git
fi
#wget https://gitee.com/open-loongarch/cross-toolchain/raw/master/gcc-13/loongarch64-linux-gnu-gcc13.3.tar.xz
#wget https://gitee.com/open-loongarch/cross-toolchain/raw/master/gcc-13/loongarch64-linux-gnu-gcc13.3.tar.xz.md5

pushd cross-toolchain/gcc-13

md5sum -c loongarch64-linux-gnu-gcc13.3.tar.xz.md5
if [ $? -eq 0 ] ; then
	sudo tar xf loongarch64-linux-gnu-gcc13.3.tar.xz -C /opt
	echo "toolchain decompress success."
else
	echo "toolchain not incomplete, please try again."
fi

popd

if [ ! -e set_env.sh ]; then
	wget https://gitee.com/open-loongarch/build-2k0300/raw/master/set_env.sh
fi

